package com.jingtai.platform.upload;


import com.jingtai.common.template.ResultMapInfo;
import com.jingtai.platform.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

@Api(tags = {"上传接口"})
@RestController
@RequestMapping("/platform")
@CrossOrigin(allowCredentials = "true")
public class UploadFileController {

    @Value("${uploadConf}")
    public String uploadConf; //配置路径
    @Value("${onlinepath}")
    public String onlinepath;//访问路径
    @Value("${uploadpath}")
    public String uploadpath;//上传地址


    @ApiOperation(value = "单图上传接口(已完成)")
    @PostMapping("/UploadFile")
    public ResultUtil UploadPicture(HttpServletResponse response, HttpServletRequest request, @RequestParam("file") MultipartFile files) throws IOException {
        ResultUtil resultUtils = new ResultUtil();
        //遍历并保存文件
        try {
            String filename = files.getOriginalFilename();
            String fileExt = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            String newFileName = df.format(new Date());
            String fileNames = newFileName + new Random().nextInt(1000) + "." + fileExt;
            // 获得文件上传日期,作为目录名一部分
            String suffix = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            String filePath = uploadpath + suffix + "/" + fileNames;
            String filepaths = "/upload/" + suffix + "/" + fileNames;
            File localFile = new File(filePath);
            if (!localFile.getParentFile().exists()) {
                localFile.getParentFile().mkdirs();
            }
            files.transferTo(localFile);
            return new ResultUtil(ResultMapInfo.OPERATE_SUCCESS, filepaths);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil(ResultMapInfo.OPERATE_FAIL);
        }
    }


    @ApiOperation(value = "多图上传接口(已完成)")
    @PostMapping("/UploadFiles")
    public ResultUtil UploadPictures(HttpServletResponse response, HttpServletRequest request, @RequestParam("file") MultipartFile[] files) throws IOException {
        ResultUtil resultUtils = new ResultUtil();
        //遍历并保存文件
        try {
            String paths = "";
            for (int i = 0; i < files.length; i++) {
                String filename = files[i].getOriginalFilename();
                String fileExt = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
                SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
                String newFileName = df.format(new Date());
                String fileNames = newFileName + new Random().nextInt(1000) + "." + fileExt;
                ;
                // 获得文件上传日期,作为目录名一部分
                String suffix = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
                String filePath = uploadpath + suffix + "/" + fileNames;
                String filepaths = "/upload/" + suffix + "/" + fileNames;
                File localFile = new File(filePath);
                if (!localFile.getParentFile().exists()) {
                    localFile.getParentFile().mkdirs();
                }
                files[i].transferTo(localFile);
                if (i == files.length - 1) {
                    paths += filepaths;
                } else {
                    paths += filepaths + ",";
                }
            }
            return new ResultUtil(ResultMapInfo.OPERATE_SUCCESS, paths);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil(ResultMapInfo.OPERATE_FAIL);
        }
    }
}
